package com.spic.business.blast.controller;

import com.spic.business.blast.domain.BlastDeployment;
import com.spic.business.blast.domain.vo.BlastAreaVo;
import com.spic.business.blast.service.IBlastDeploymentService;
import com.spic.business.common.datascope.anotation.BusinessDataScope;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 爆破布防Controller
 *
 * @author spic
 * @date 2022-03-03
 */
@RestController
@RequestMapping("/blast")
public class BlastDeploymentController extends BaseController
{
    @Autowired
    private IBlastDeploymentService blastDeploymentService;
    private static final Logger log = LoggerFactory.getLogger(BlastDeploymentController.class);
    /**
     * 查询爆破布防列表
     */
    @PreAuthorize(hasPermi = "deployment:blast:list")
    @BusinessDataScope(menuPath = "business/blastDeployment/index")
    @GetMapping("/list")
    public TableDataInfo list(BlastDeployment blastDeployment)
    {
        startPage();
        List<BlastDeployment> list = blastDeploymentService.selectBlastDeploymentList(blastDeployment);
        return getDataTable(list);
    }

    @GetMapping("/holes")
    public List<BlastAreaVo> listAll(BlastDeployment blastDeployment, HttpServletResponse httpServletResponse )
    {
        httpServletResponse.addHeader("Access-Control-Allow-Origin", "*");
        return blastDeploymentService.getPolygons();
    }

    //app端 调度确认按钮
    @PreAuthorize(hasPermi = "deployment:blast:confirmation")
    @PutMapping("/confirmation/update")
    public AjaxResult confirmationUpdate(@RequestBody BlastDeployment blastDeployment)
    {
        return toAjax(blastDeploymentService.updateBlastDeployment(blastDeployment));
    }

    /**
     * 导出爆破布防列表
     */
    @PreAuthorize(hasPermi = "deployment:blast:export")
    @Log(title = "爆破布防", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlastDeployment blastDeployment) throws IOException
    {
        List<BlastDeployment> list = blastDeploymentService.selectBlastDeploymentList(blastDeployment);
        ExcelUtil<BlastDeployment> util = new ExcelUtil<BlastDeployment>(BlastDeployment.class);
        util.exportExcel(response, list, "爆破布防数据");
    }

    /**
     * 获取爆破布防详细信息
     */
    @PreAuthorize(hasPermi = "deployment:blast:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(blastDeploymentService.selectBlastDeploymentById(id));
    }

    @PreAuthorize(hasPermi = "deployment:blast:detail")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getInfoDetail(@PathVariable("id") String id)
    {
        return AjaxResult.success(blastDeploymentService.selectBlastDeploymentById(id));
    }

    /**
     * 新增爆破布防
     */
    @PreAuthorize(hasPermi = "deployment:blast:add")
    @Log(title = "爆破布防", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlastDeployment blastDeployment)
    {
        return toAjax(blastDeploymentService.insertBlastDeployment(blastDeployment));
    }

    /**
     * 修改爆破布防
     */
    @PreAuthorize(hasPermi = "deployment:blast:edit")
    @Log(title = "爆破布防", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlastDeployment blastDeployment)
    {
        return toAjax(blastDeploymentService.updateBlastDeployment(blastDeployment));
    }

    /**
     * 删除爆破布防
     */
    @PreAuthorize(hasPermi = "deployment:blast:remove")
    @Log(title = "爆破布防", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(blastDeploymentService.deleteBlastDeploymentByIds(ids));
    }

    /**
     * 删除孔位 信息根据id
     * @param ids
     * @return
     */

    @DeleteMapping("/chargPositionByid/{ids}")
    public AjaxResult removeChargPositionByid(@PathVariable("ids") String ids)
    {
        return toAjax(blastDeploymentService.deleteChargPositionByidByIds(ids));
    }

    @GetMapping("/code")
    public AjaxResult getCode(){
        return AjaxResult.success(blastDeploymentService.getCode());
    }

    @GetMapping("/getBlastDeploymentListByDate")
    public AjaxResult getBlastDeploymentListByDate(String date){
        List<BlastDeployment> list = blastDeploymentService.selectBlastDeploymentListByDate(date);
        return AjaxResult.success(list);
    }

    @GetMapping("/getCameraIndexCodeType")
    public AjaxResult getCameraIndexCodeType(){
        List<Map<String,Object>> map = blastDeploymentService.getCameraIndexCodeType();
        return AjaxResult.success(map);
    }

    @GetMapping("calcAlarm")
    public void calcAlarm()
    {
        blastDeploymentService.calcAlarm();
    }

    @GetMapping("filterDirtyData")
    public AjaxResult filterDirtyData() {
        blastDeploymentService.filterDirtyData();
        return AjaxResult.success();
    }

}
